package com.jacky.iot.persist;

import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class DefaultMqttPersistenceFactory implements MqttPersistenceFactory {

    private MqttClientPersistence persistence;

    @Override
    public MqttClientPersistence clientPersistence() {
        synchronized (DefaultMqttPersistenceFactory.class) {
            if (Objects.isNull(persistence)) {
                synchronized (DefaultMqttPersistenceFactory.class) {
                    persistence = new MemoryPersistence();
                }
            }
        }
        return this.persistence;
    }
}
